import 'dart:io';
import 'package:fate_blind_box/utils/common_widget.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import '../../../../theme/theme_color.dart';
import '../../../../utils/photo_utils/photo_tool.dart';
import '../../../Home/activity/act_details_pre_page.dart';
import '../../../Home/activity/act_list_model.dart';
import '../rich_editor/act_editor_page.dart';
import 'act_publish_bottom_view.dart';
import 'act_publish_controller.dart';
import 'act_publish_item_view.dart';
import 'act_publish_switch_item_view.dart';

class ActPublishPage extends StatefulWidget {
  const ActPublishPage({Key? key}) : super(key: key);

  @override
  State<ActPublishPage> createState() => _ActPublishPageState();
}

class _ActPublishPageState extends State<ActPublishPage> {
  ActPubListController publishController = Get.put(ActPubListController());
  double leftSpace = 14.w;
  Color lineColor = const Color(0XFFF4F5F7);
  FuturePopular saveModel = FuturePopular();

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        AppWidget.navTitleBar(
            bgColor: Colors.white,
            title: '发活动',
            backAction: () {
              publishController.clickPrelishAction();
            },
            childWidget: ListView(
              children: [
                Container(
                  height: 8,
                  color: lineColor,
                ),
                GetBuilder(
                    id: publishController.photoCoverId,
                    builder: (ActPubListController controller) {
                      ImageProvider imageProvider;
                      if (controller.photoCoverUrl.contains('http')) {
                        imageProvider = NetworkImage(controller.photoCoverUrl);
                      } else {
                        imageProvider =
                            FileImage(File(controller.photoCoverUrl));
                      }

                      return GestureDetector(
                        onTap: () {
                          //图片点击事件
                          _selectPhoto(context, 1.sw, isCover: true);
                        },
                        child: Container(
                            margin: EdgeInsets.only(
                                top: leftSpace,
                                left: leftSpace,
                                right: leftSpace),
                            height: 203,
                            width: 1.sw - leftSpace * 2,
                            alignment: Alignment.center,
                            decoration: controller.photoCoverUrl.isNotEmpty
                                ? BoxDecoration(
                                    color: const Color.fromRGBO(
                                        248, 246, 253, 1.0),
                                    borderRadius: BorderRadius.circular(5),
                                    image: DecorationImage(
                                      fit: BoxFit.cover,
                                      image: imageProvider,
                                    ),
                                  )
                                : BoxDecoration(
                                    color: const Color.fromRGBO(
                                        248, 246, 253, 1.0),
                                    borderRadius: BorderRadius.circular(5),
                                  ),
                            child: Visibility(
                              visible: controller.photoCoverUrl.isEmpty,
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.center,
                                mainAxisAlignment: MainAxisAlignment.center,
                                children: [
                                  Image.asset(
                                    "images/ic_qiyeminiPhoto.png",
                                    width: 37,
                                    height: 31,
                                    fit: BoxFit.cover,
                                  ),
                                  const SizedBox(
                                    height: 5,
                                  ),
                                  Text(
                                    '+添加海报',
                                    style: TextStyle(
                                      color: ThemeColor.grayB5B5B5Color,
                                      fontSize: 16,
                                    ),
                                  ),
                                  const SizedBox(
                                    height: 15,
                                  ),
                                  Text(
                                    '高颜值的图片能帮助您获得更多的曝光',
                                    style: TextStyle(
                                      color: ThemeColor.grayB5B5B5Color,
                                      fontSize: 12,
                                    ),
                                  ),
                                  const SizedBox(
                                    height: 5,
                                  ),
                                  Text(
                                    '建议宽高比例为347：203',
                                    style: TextStyle(
                                      color: ThemeColor.grayB5B5B5Color,
                                      fontSize: 12,
                                    ),
                                  ),
                                ],
                              ),
                            )),
                      );
                    }),
                ActPublishItemView(
                  leftName: '活动主题',
                  placeName: '请输入活动主题',
                  inputController: publishController.actTitleVC,
                  isCanEdit: true,
                ),
                GetBuilder(
                    id: publishController.startTimeId,
                    builder: (ActPubListController controller) {
                      return ActPublishItemView(
                        leftName: '开始时间',
                        placeName: '请输入开始时间',
                        inputController: publishController.startTimeVC,
                        callBack: () {
                          publishController.choseTimeView();
                        },
                      );
                    }),
                GetBuilder(
                    id: publishController.endTimeId,
                    builder: (ActPubListController controller) {
                      return ActPublishItemView(
                        leftName: '结束时间',
                        placeName: '请输入结束时间',
                        inputController: publishController.endTimeVC,
                        callBack: () {
                          publishController.choseTimeView(isEndTime: true);
                        },
                      );
                    }),
                GetBuilder(
                    id: publishController.addressViewId,
                    builder: (ActPubListController controller) {
                      return ActPublishItemView(
                        leftName: '活动地点',
                        placeName: '请选择活动地点',
                        inputController: publishController.addressVC,
                        callBack: () {
                          publishController.chosePlaceView();
                        },
                      );
                    }),
                GetBuilder(
                    id: publishController.expenseViewId,
                    builder: (ActPubListController controller) {
                      return ActPublishItemView(
                        leftName: '活动费用',
                        placeName: '请选择费用',
                        inputController: publishController.expenseVC,
                        callBack: () {
                          publishController.showChoseExpense();
                        },
                      );
                    }),
                ActPublishItemView(
                  leftName: '详情描述',
                  placeName: '请编辑',
                  callBack: () {
                    Get.to(ActEditorPage(
                      savAction: (res) {
                        publishController.actDetailsDes = res;
                        publishController.actDetailsVC.text=res;
                      },
                      jsonStr: publishController.actDetailsDes,
                    ));
                  },
                ),
                Container(
                  height: 16,
                  color: lineColor,
                ),
                GetBuilder(
                    id: publishController.publishToHomeId,
                    builder: (ActPubListController controller) {
                      return ActPubLishSwitchItemView(
                        leftName: '发布到首页',
                        isChose: controller.isPublishToHome,
                        switchAction: () {
                          controller.isPublishToHome =
                              !controller.isPublishToHome;
                          controller.update([controller.publishToHomeId]);
                        },
                      );
                    }),
                GetBuilder(
                    id: publishController.publishToMyGroupId,
                    builder: (ActPubListController controller) {
                      return ActPubLishSwitchItemView(
                        leftName: '发布到我的群',
                        isHiddenLine: true,
                        isChose: controller.isPublishToMyGroup,
                        switchAction: () {
                          controller.isPublishToMyGroup =
                              !controller.isPublishToMyGroup;
                          controller.update([controller.publishToMyGroupId]);
                        },
                      );
                    }),
                Container(
                  height: 84,
                  color: Colors.white,
                ),
              ],
            )),
        Positioned(
          bottom: 0,
          left: 0,
          right: 0,
          child: ActPubLishBottomView(
            leftAction: () {
              if (publishController.actTitleVC.text.isNotEmpty) {
                publishController.preViewModel.actiTitle =
                    publishController.actTitleVC.text;
              }
              Get.to(ActDetailsPrePage(
                itemModel: publishController.preViewModel,
              ));
            },
            rightAction: () {
              publishController.clickPublishAction();
            },
          ),
        ),
      ],
    );
  }

  Future<void> _selectPhoto(context, double width,
      {bool isCover = true}) async {
    showModalBottomSheet(
      context: context,
      builder: (BuildContext context) {
        return SizedBox(
            height: 220.0,
            child: Container(
                alignment: Alignment.center,
                child: Column(
                  children: [
                    GestureDetector(
                        onTap: () {
                          Navigator.pop(context);
                          PhotoTool.getPhotoImgFile('拍照', context,
                              callBack: (url) {
                            publishController.choseCoverAndDetails(url);
                          }, isNoNeedCrop: true);
                        },
                        child: Container(
                          alignment: Alignment.center,
                          width: width,
                          height: 55,
                          color: Colors.white,
                          child: const Text('拍照'),
                        )),
                    Divider(
                      color: ThemeColor.sendMsgColor,
                      height: 1,
                    ),
                    GestureDetector(
                        onTap: () {
                          Navigator.pop(context);
                          PhotoTool.getPhotoImgFile('相册', context,
                              callBack: (url) {
                            publishController.choseCoverAndDetails(url);
                          }, isNoNeedCrop: true);
                        },
                        child: Container(
                          alignment: Alignment.center,
                          width: width,
                          height: 60,
                          color: Colors.white,
                          child: const Text('从手机相册选择'),
                        )),
                    Divider(
                      color: ThemeColor.sendMsgColor,
                      thickness: 7,
                      height: 1,
                    ),
                    GestureDetector(
                      onTap: () {
                        Navigator.pop(context);
                      },
                      child: Container(
                        alignment: Alignment.center,
                        width: width,
                        height: 55,
                        child: const Text('取消'),
                      ),
                    ),
                  ],
                )));
      },
    );
  }

  @override
  void initState() {
    super.initState();
    publishController.getActPreviewDetails();
  }


}
